package br.ufpe.cin.rgms.aspects;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import br.ufpe.cin.rgms.util.HibernateUtil;

public privileged aspect PersistenceSwapBehaviour {
	void around(String type) : execution(void HibernateUtil.swapPersistence(String)) && args(type){
		if (type.equals("postgresql")) {
			System.out.println("============postgresql============");
			HibernateUtil.factory.close();
			HibernateUtil.conf = new AnnotationConfiguration();
			HibernateUtil.addAnnotatedClasses();
			PostgresqlPersistenceInitialization.changeConfigurationProperties();
			HibernateUtil.factory = HibernateUtil.conf.buildSessionFactory();
		} else if (type.equals("mysql")) {
			System.out.println("============mysql============");
			HibernateUtil.factory.close();
			HibernateUtil.conf = new AnnotationConfiguration();
			HibernateUtil.addAnnotatedClasses();
			MysqlPersistenceInitialization.changeConfigurationProperties();
			HibernateUtil.factory = HibernateUtil.conf.buildSessionFactory();
		}
		proceed(type);
	}
}
